CoCalc Logo Icon
DocsShareSupport News Sign UpSign In
Views: 100
Image: ubuntu2004
Embed | Download | Raw |
Kernel: Python 3 (ipykernel)
import random from IPython.core.display import SVG import pyomo.environ as pyo from pysat.solvers import Solver from pysat.formula import CNF import py_svg_combinatorics as psc from ipywidgets import widgets, HBox from collections import Counter from pprint import pprint from random import randint import numpy as np from IPython.display import IFrame import IPython from copy import copy import os from pathlib import Path np.set_printoptions(precision=2) np.set_printoptions(suppress=True) nbname = '' try: nbname = __vsc_ipynb_file__ except: if 'COCALC_JUPYTER_FILENAME' in os.environ: nbname = os.environ['COCALC_JUPYTER_FILENAME'] title_ = Path(nbname).stem.replace('-', '_').title() IFrame(f'https://discopal.ispras.ru/index.php?title=Hardprob/{title_}&useskin=cleanmonobook', width=1280, height=300)
# Случайные данные m = np.random.randint(3, 20) n = np.random.randint(3, 20) A = np.random.rand(m, n) c = np.random.rand(n) b = 1/np.random.rand(m) A, b, c
(array([[0.28, 0.04, 0.76], [0.06, 0.17, 0.85], [0.31, 0.82, 0.01], [0.46, 0.76, 0.13], [0.82, 0.48, 0.67], [0.77, 0.28, 0.28], [0.09, 0.86, 0.62], [0.56, 0.76, 0.83], [0.01, 0.94, 0.3 ], [0.01, 0.86, 0.33], [0.59, 0.91, 0.91], [0.94, 0.28, 0.13]]), array([10.21, 2.84, 3.5 , 1.52, 4.71, 1.77, 1.16, 2.74, 33.54, 1.65, 4.09, 7.72]), array([0.19, 0.02, 0.93]))

def get_model(A, b, c): m = pyo.ConcreteModel() m.m, m.n = A.shape # на всякий случай, возьмем с собой m.A = A m.b = b m.c = c m.I = range(m.m) m.J = range(m.n) m.x = pyo.Var(m.J, domain=pyo.Binary) m.obj = pyo.Objective(expr = sum( c[j] * m.x[j] for j in m.J), sense=pyo.maximize) @m.Constraint(m.I) def не_больше(m, i): return sum(A[i, j] * m.x[j] for j in m.J) <= b[i] return m m = get_model(A, b, c) m.pprint()
2 Set Declarations x_index : Size=1, Index=None, Ordered=Insertion Key : Dimen : Domain : Size : Members None : 1 : Any : 3 : {0, 1, 2} не_больше_index : Size=1, Index=None, Ordered=Insertion Key : Dimen : Domain : Size : Members None : 1 : Any : 12 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} 1 Var Declarations x : Size=3, Index=x_index Key : Lower : Value : Upper : Fixed : Stale : Domain 0 : 0 : None : 1 : False : True : Binary 1 : 0 : None : 1 : False : True : Binary 2 : 0 : None : 1 : False : True : Binary 1 Objective Declarations obj : Size=1, Index=None, Active=True Key : Active : Sense : Expression None : True : maximize : 0.18665218449400867*x[0] + 0.022131302476188552*x[1] + 0.9332152905432948*x[2] 1 Constraint Declarations не_больше : Size=12, Index=не_больше_index, Active=True Key : Lower : Body : Upper : Active 0 : -Inf : 0.28055995083896923*x[0] + 0.04311338848771118*x[1] + 0.7553328617192127*x[2] : 10.208509520005157 : True 1 : -Inf : 0.05706343336067832*x[0] + 0.17223169829373708*x[1] + 0.851709606386633*x[2] : 2.841947518933711 : True 2 : -Inf : 0.31410422998134035*x[0] + 0.8218998160461224*x[1] + 0.013382000608938571*x[2] : 3.500710635040607 : True 3 : -Inf : 0.4571955790866228*x[0] + 0.7557608108832564*x[1] + 0.12763796282695694*x[2] : 1.5170733122487856 : True 4 : -Inf : 0.8241388555644097*x[0] + 0.47968596423293164*x[1] + 0.6743732402516242*x[2] : 4.708897654861087 : True 5 : -Inf : 0.7669757488481207*x[0] + 0.2751077097585235*x[1] + 0.28411090506692693*x[2] : 1.7746982901073594 : True 6 : -Inf : 0.0861936024697626*x[0] + 0.8637421504128335*x[1] + 0.6214915614192364*x[2] : 1.1574234570440058 : True 7 : -Inf : 0.562352580503253*x[0] + 0.7625513515097191*x[1] + 0.8332506030983015*x[2] : 2.743714198711757 : True 8 : -Inf : 0.0059528686189228575*x[0] + 0.939683705400966*x[1] + 0.2989939670427869*x[2] : 33.54141010868532 : True 9 : -Inf : 0.009863211936013694*x[0] + 0.8629277160965173*x[1] + 0.33460266014037754*x[2] : 1.6533384822754165 : True 10 : -Inf : 0.592348928334222*x[0] + 0.9147718009652097*x[1] + 0.913500080611226*x[2] : 4.094888292619038 : True 11 : -Inf : 0.9419901602543408*x[0] + 0.2804426976738712*x[1] + 0.12638600466485816*x[2] : 7.721759169896813 : True 5 Declarations: x_index x obj не_больше_index не_больше
def print_solution(m): for v in m.component_data_objects(pyo.Var): if v.value and v.value > 0: print(str(v), v.value)
solver = pyo.SolverFactory('cbc') solver.solve(m).write() print_solution(m)
# ========================================================== # = Solver Results = # ========================================================== # ---------------------------------------------------------- # Problem Information # ---------------------------------------------------------- Problem: - Name: unknown Lower bound: 1.11986748 Upper bound: 1.11986748 Number of objectives: 1 Number of constraints: 1 Number of variables: 3 Number of binary variables: 3 Number of integer variables: 3 Number of nonzeros: 3 Sense: maximize # ---------------------------------------------------------- # Solver Information # ---------------------------------------------------------- Solver: - Status: ok User time: -1.0 System time: 0.0 Wallclock time: 0.0 Termination condition: optimal Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available. Statistics: Branch and bound: Number of bounded subproblems: 0 Number of created subproblems: 0 Black box: Number of iterations: 1 Error rc: 0 Time: 0.041793107986450195 # ---------------------------------------------------------- # Solution Information # ---------------------------------------------------------- Solution: - number of solutions: 0 number of solutions displayed: 0 x[0] 1.0 x[2] 1.0